package utils;

import constant.Constant;
import entry.P;
import entry.Page;
import entry.Rule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class QiMaDingUtil {
    public static void main(String[] args) {
//        List<List<P>> lists = getLists(16, 1, 2, 2, Constant.yin_shua_fang_shi_zheng_fan);
//        getPwiths("1* 4* 2 3", 8, false);
        List<List<P>> 正反 = getLists(8, 1, 2, 2, "正反", false);
        System.out.println(正反);
    }


    private static List<List<P>> getPwiths(String rule, int pages, boolean is_zheng_fan) {
        List<List<P>> lists = new ArrayList<>();
        String[] arr_rule = rule.split(" ");
        int page_count = arr_rule.length;
        int cs = pages / page_count;
        if(cs==0){
            cs = 1;
        }
        for (int i = 0; i < cs; i++) {
            //正反面
            List<P> font_list = new ArrayList<>();
            List<P> back_list = new ArrayList<>();
            for (int j = 0; j < arr_rule.length; j++) {
                if (is_zheng_fan) {
                    //正面
                    String item = arr_rule[j];
                    if (item.endsWith("*")) {
                        int temp_page = Integer.valueOf(item.substring(0, item.length() - 1));
                        if (temp_page > page_count / 2) {
                            if (j < arr_rule.length / 2) {
                                font_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 180));
                            } else {
                                back_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 180));
                            }
                        } else {
                            if (j < arr_rule.length / 2) {
                                font_list.add(new P(temp_page + i * page_count / 2, 180));
                            } else {
                                back_list.add(new P(temp_page + i * page_count / 2, 180));
                            }
                        }
                    } else {
                        int temp_page = Integer.valueOf(item);
                        if (temp_page > page_count / 2) {
                            if (j < arr_rule.length / 2) {
                                font_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 0));
                            } else {
                                back_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 0));
                            }
                        } else {
                            if (j < arr_rule.length / 2) {
                                font_list.add(new P(temp_page + i * page_count / 2, 0));
                            } else {
                                back_list.add(new P(temp_page + i * page_count / 2, 0));
                            }
                        }
                    }
                } else {
                    String item = arr_rule[j];
                    if (item.endsWith("*")) {
                        int temp_page = Integer.valueOf(item.substring(0, item.length() - 1));
                        if (temp_page > page_count / 2) {
                            font_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 180));
                        } else {
                            font_list.add(new P(temp_page + i * page_count / 2, 180));
                        }
                    } else {
                        int temp_page = Integer.valueOf(item);
                        if (temp_page > page_count / 2) {
                            font_list.add(new P(pages - (page_count - temp_page) - i * page_count / 2, 0));
                        } else {
                            font_list.add(new P(temp_page + i * page_count / 2, 0));
                        }
                    }
                }
            }
            if (is_zheng_fan) {


                lists.add(font_list);

                lists.add(back_list);


            } else {
                lists.add(font_list);
            }
        }
        System.out.println(lists);
        return lists;
    }


    public static List<List<P>> getLists(int page, int lian, int col, int row, String ysfs,boolean is_heng) {
        System.out.println("lian:" + lian + "page:" + page + " " + col + "x" + row);
        String rule = Rule.getRule(is_heng, ysfs.equals("正反"), col, row, lian, page);
        return getPwiths(rule,page,ysfs.equals("正反"));

    }


    public static List<Page> getPages(int page, int cr) {
        int a, b, c, d, e;
        e = page;
        List<Integer> list1 = new LinkedList<>();
        a = cr;
        if (cr % 6 == 0) {
            d = 6;
        } else if (cr % 4 == 0) {
            d = 4;
        } else {
            return null;
        }
        do {
            b = a / d;
            c = page / a;
            if (cr % b == 0) {
                for (int i = 0; i < c; i++) {
                    list1.add(a);
                }
                page = page - a * c;
            }
            a = (b - 1) * d;

        } while (page > 0);
        Collections.reverse(list1);
        List<Page> pageList = new ArrayList<>();
        Page pageObj = null;
        List<Integer> list2 = null;
        int multiple, f;
        int start = 1, end = e;
        boolean flag = false;
        for (int i : list1) {
            list2 = new ArrayList<>();
            pageObj = new Page();
            multiple = cr / i;
            if (i < cr) {
                f = i / 2;
                for (int j = 0; j < f; j++) {
                    list2.add(start + j);
                }
                for (int j = f; j > 0; j--) {
                    list2.add(end - j + 1);
                }
                start = start + f;
                end = end - f;
            } else {
                for (Page page1 : pageList) {
                    if (page1.getMultiple() == 1) {
                        flag = true;
                        list2 = page1.getPages();
                        break;
                    }
                }
                for (int j = 0; j < cr; j++) {
                    list2.add(start + j);
                }
                start = start + cr;
            }
            if (!flag) {
                pageObj.setPages(list2);
                pageObj.setMultiple(multiple);
                pageList.add(pageObj);
            }
        }
        return pageList;
    }

}
